package GUI;

import Logica.AgregarSupp;
import Logica.ModificarSupp;
import Logica.Supplies;

import java.awt.Color;
import java.awt.Dimension;

import java.awt.Font;
import java.awt.Rectangle;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class Inventario extends JFrame {
  private JButton jBAgregarInv = new JButton();
  private JButton jBModificarInv = new JButton();
  private JLabel jLInventario = new JLabel();
  private JTable jTInventario = new JTable();
  private JButton jBEliminar = new JButton();
  Supplies supp=new Supplies();

  public Inventario() {
    try {
      jbInit();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  private void jbInit() throws Exception {
    this.getContentPane().setLayout( null );
    this.setSize(new Dimension(785, 542));
    this.setTitle("INVENTARIO");
    jBAgregarInv.setText("AGREGAR");
    jBAgregarInv.setBounds(new Rectangle(400, 460, 110, 35));
    jBAgregarInv.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          jBAgregarInv_actionPerformed(e);
        }
      });
    jBModificarInv.setText("MODIFICAR");
    jBModificarInv.setBounds(new Rectangle(645, 460, 110, 35));
    jBModificarInv.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          jBModificarInv_actionPerformed(e);
        }
      });
    jLInventario.setText("INVENTARIO");
    jLInventario.setBounds(new Rectangle(295, 15, 170, 65));
    jLInventario.setFont(new Font("ALGERIAN", 0, 30));
    jLInventario.setForeground(new Color(49, 49, 255));
    jBEliminar.setText("ELIMINAR");
    jBEliminar.setBounds(new Rectangle(525, 460, 110, 35));
    jBEliminar.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          jBEliminar_actionPerformed(e);
        }
      });
    this.getContentPane().add(jBEliminar, null);
    this.getContentPane().add(jLInventario, null);
    this.getContentPane().add(jBModificarInv, null);
    this.getContentPane().add(jBAgregarInv, null);
    Object[][] info = supp.DarDatosSupplies();
    String[] tit =
    { "TREATMENT NUMBER", "DESCRIPTION", "TCOST", "QUANTITY", "OBSERVATION",
      "BEGINDATE", "END DATE", "EXAMINATION NUMBER" };
    DefaultTableModel dtm = new DefaultTableModel(info, tit);

    jTInventario = new JTable(dtm);

    jTInventario.setPreferredScrollableViewportSize(new Dimension(300,
                                                                     100)); // las dimensiones
    jTInventario.setBounds(new Rectangle(10, 105, 690, 350));
    jTInventario.setSize(new Dimension(750, 350));
    this.getContentPane().add(jTInventario,null);
    
  }

  private void jBAgregarInv_actionPerformed(ActionEvent e) {
    AgregarSupp Asup=new AgregarSupp();
    Asup.setVisible(true);
  }

  private void jBEliminar_actionPerformed(ActionEvent e) {
    int fila = jTInventario.getSelectedRow();
    
    Object[] info = new Object[10];
    for (int i = 0; i < jTInventario.getColumnCount(); i++){
      info[i] = jTInventario.getValueAt(fila, i);
    }
    System.out.println(info[0]);
    if(supp.EliminarSupp(String.valueOf(info[0])))
    {
      JOptionPane.showMessageDialog(null, "Eliminado", "El dato ha sido eliminado", JOptionPane.INFORMATION_MESSAGE); 
      }
    else{
        JOptionPane.showMessageDialog(null, "No Eliminado",
                                      "El tratamiento no ha sido Eliminado",
                                      JOptionPane.ERROR_MESSAGE);
      }
     
  }

  private void jBModificarInv_actionPerformed(ActionEvent e) {
    int fila = jTInventario.getSelectedRow();
    System.out.println(fila);
    Object[] info = new Object[10];
    for (int i = 0; i < jTInventario.getColumnCount(); i++){
      info[i] = jTInventario.getValueAt(fila, i);
    }
    ModificarSupp modS= new ModificarSupp(info);
    modS.setVisible(true);
  }
}
